package com.example.demo.Fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.demo.R;
import com.example.demo.databinding.FragmentAccountBinding;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link AccountFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class AccountFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private FragmentAccountBinding  binding;
    private FragmentActivity activity;

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private ChildRole childRoleFragment;
    private ChildUser childUserFragment;
    private Fragment currentChildFragment;

    public AccountFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment AccountFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static AccountFragment newInstance(String param1, String param2) {
        AccountFragment fragment = new AccountFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        binding = FragmentAccountBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        activity = requireActivity();
        setupTabListenners();
        UserOnclick();

        setupInitialFragment(savedInstanceState);
    }

    private void setupTabListenners() {

        TextView dataText = binding.dataText;
        TextView auditText = binding.auditText;
        dataText.setSelected(true);

        //tab切换
        dataText.setOnClickListener(v -> {
            if (!dataText.isSelected()) {
                dataText.setSelected(true);
                auditText.setSelected(false);
                switchTab(0);
            }

        });
        auditText.setOnClickListener(v -> {
            if (!auditText.isSelected()) {
                auditText.setSelected(true);
                dataText.setSelected(false);
                switchTab(1);
            }
        });
    }
    private void UserOnclick() {
        //关闭当前页
        binding.icon.setOnClickListener(v -> {
            activity.getSupportFragmentManager().popBackStack();
        });
    }

    private void setupInitialFragment(Bundle savedInstanceState) {
        if (savedInstanceState == null) {
            childUserFragment = new ChildUser();
            getChildFragmentManager().beginTransaction()
                    .add(R.id.fragment_child, childUserFragment)
                    .commit();
            currentChildFragment = childUserFragment;
        } else {
            childUserFragment = (ChildUser) getChildFragmentManager().findFragmentByTag(ChildUser.class.getSimpleName());
            childRoleFragment = (ChildRole) getChildFragmentManager().findFragmentByTag(ChildRole.class.getSimpleName());
        }
    }

    private void switchTab(int tabIndex) {
        Fragment toShowFragment;

        if (tabIndex == 0) {
            if (childUserFragment == null) {
                childUserFragment = new ChildUser();
                getChildFragmentManager().beginTransaction()
                        .add(R.id.fragment_child, childUserFragment, ChildUser.class.getSimpleName())
                        .hide(childUserFragment)
                        .commitNow();
            }
            toShowFragment = childUserFragment;
        } else {
            if (childRoleFragment == null) {
                childRoleFragment = new ChildRole();
                getChildFragmentManager().beginTransaction()
                        .add(R.id.fragment_child, childRoleFragment, ChildRole.class.getSimpleName())
                        .hide(childRoleFragment)
                        .commitNow();
            }
            toShowFragment = childRoleFragment;
        }

        if (currentChildFragment != null && currentChildFragment != toShowFragment) {
            getChildFragmentManager().beginTransaction()
                    .hide(currentChildFragment)
                    .show(toShowFragment)
                    .commitNow();
        }

        currentChildFragment = toShowFragment;
    }

}